<template>
    <div>
        <el-card class="box-card" >
            <CommonPageNew
                    ref='formPage'
                    :pageParam="pageParam"
                    @updatePagination="updatePagination"
                    :callbackParam.sync="callbackParam"/>
        </el-card>
        <ComDialog
                :dialog="dialog"
                :visable.sync="dialog.visable">
            <ComForm :formDesign="formDesign" :dialog="dialog"
                     ref='form'
                     :form.sync="form"
                     :btns="btns"></ComForm>
        </ComDialog>
        
    </div>
</template>
<script>
import {throttle} from "@/utils/index"
import {actionByTypeNew} from '@/utils/pageComMethods'
import {
  relation,
} from '@/api/admin'
export default {
  props:{
        pageParamAll:{
            type:Object,
            required:true,
            default:{
            }
        },
    },
    data(){
        return {
            pageParam:{},
            dialog:{
                title:'角色配置',
                visable:false,
                width:'40%'
            },
            formDesign:{},
            form:{},
            btns:[],
            callbackParam:{},

            
        }
    },
    created(){
        this.initPageParam();
        this.initForm();
    },
    watch:{
      "dialog.visable":function(n,o){
          if(!n){
             this.form={};
             this.$refs.form.clear()
          }
        }
    },
    methods:{
        

        initForm:function(){
            var that = this ;
            this.formDesign=this.pageParamAll.formDesign
            this.btns=this.pageParamAll.btns
        },
        delete : function(row){
            this.$confirm(`是否确定删除${this.pageParam.initData.actionName}？`,"提示",{
                confirmButtonText:"确定删除"
            }).then(()=>{
                var id ;
                if(this.pageParam.initData.delUrl.url==="/account/"){
                    //管理员删除
                    id = row.accountId
                }else{//其他
                    id = row.id
                }
                this.throttleServe(this.pageParam.initData.delUrl.methods,{},this.pageParam.initData.delUrl.url+id,this,"delete",this.pageParam.initData.actionName)
            })
        },
        deleteAll : function(){
            var arr = this.callbackParam.multipleSelection ;
            if(!arr ||  arr.length < 1){
                this.$message({
                    type:"warning",
                    message:"最少选中一条数据"
                });
                return ;
            }
            var temp = [];
            for(var i in arr){
                temp.push(arr[i].id);
            }
            this.$confirm(`是否确定批量删除${this.pageParam.initData.actionName}？`,"提示",{
                confirmButtonText:"确定删除"
            }).then(()=>{
                this.throttleServe("post",temp,"/api/role/removeByIds",this,"deleteAll",this.pageParam.initData.actionName)
            })

        },
        saveForm:function(){
            var flag = this.$refs.form.validateForm();
            if(!flag)return;
            var temp = this.form;
            // console.log({temp})
            if(this.dialog.title.includes("添加")){
              this.throttleServe(this.pageParam.initData.updateUrl.methods,temp,this.pageParam.initData.updateUrl.url,this,"update",this.pageParam.initData.actionName)
            }else if(this.dialog.title.includes("编辑")){
              this.throttleServe(this.pageParam.initData.addUrl.methods,temp,this.pageParam.initData.addUrl.url,this,"add",this.pageParam.initData.actionName)
            }else if(this.dialog.title.includes("绑定角色")){
                this.throttleServe(this.pageParam.initData.bindUrl.methods,temp,this.pageParam.initData.bindUrl.url,this,"bind",this.dialog.title)
            }
        },
        throttleServe: throttle((methods, params, url, that,from,name) => {
          //from 来自于哪个按钮 如编辑update删除delete新增add等
          //name 需要操作的名称 
          console.log('methods', methods)
          actionByTypeNew(methods, url, params,that,from,name)
        }, 1000),
        renderPage:function(){
            this.initPageParam();
        },
        
        initPageParam : function(){
            var that = this ;
            this.pageParam=this.pageParamAll.pageParam
            
        },
        viewForm : function(row,type){
            //type 添加 编辑 绑定角色
            if(!row){
                this.form = {
                    // id: this.uuid(),
                };
                this.dialog.title = this.pageParam.initData.actionName+"添加" ;
            }else{
                this.form = Object.assign({},row);
                if(!type||type==="编辑"){
                    this.dialog.title = this.pageParam.initData.actionName+"编辑" ;
                }else{
                    this.dialog.title = type ;
                    if(type==="绑定角色"){
                        // this.form.roles=
                        this.getRelation(row.accountId)
                    }
                }
                // this.dialog.title = this.pageParam.initData.actionName+"编辑" ;
            }
            this.dialog.visable = true ;   
        },
        updatePagination(pagination){
            this.pageParam.initData.pagination=pagination
        },
        //管理员绑定角色查询
        getRelation (accountId) {
        let selectRoles = []
            relation(accountId).then(response => {
                if(response.data.code==200){
                let data = response.data.data
                this.roleOptions = response.data.data
                data.forEach(v => {
                    if (v.checked) {
                    selectRoles.push(v.roleId)
                    }
                })
                this.$set(this.form,"selectRoles",selectRoles)
                // this.form.roles== selectRoles
                // this.dialogFormVisible = true
                }
            })
        },
    }

}


</script>
<style scoped>




</style>





























